package com.rf.richfitwheel.common.utils;


import com.rf.richfitwheel.common.enums.ResultStatus;
import com.rf.richfitwheel.common.errors.ResultCache;

public class Result<T> {

	//业务错误码
	private Integer code;
	// 信息描述
	private String msg;
	//返回参数
	private T data;

	public Result() {
	}


	private Result(ResultStatus resultStatus, T data) {
		this.code = resultStatus.getCode();
		this.msg = resultStatus.getMessage();
		this.data = data;
	}

	/**返回自定义的code和msg：ResultStatus可以自定义msg  */
	public static Result  success(ResultStatus resultStatus){
		Result result=new Result();
		result.setCode(resultStatus.getCode());
		result.setMsg(resultStatus.getMessage());
		return result;
	}

	private Result(R r, T data) {
		this.code = Integer.valueOf(r.getCode());
		this.msg = r.getMsg();
		this.data = data;
	}

	/** 业务成功返回业务代码和描述信息 */
	public static Result<Void> success() {
		return new Result<>(ResultStatus.SUCCESS, null);
	}

	/** 业务成功返回业务代码,描述和返回的参数 */
	public static <T> Result<T> success(T data) {
		return new Result<>(ResultStatus.SUCCESS, data);
	}

	/** 业务成功返回业务代码,描述和返回的参数 */
	public static <T> Result<T> success(ResultStatus resultStatus, T data) {
		if (resultStatus == null) {
			return success(data);
		}
		return new Result<>(resultStatus, data);
	}

	/*public static Result failure(ResultStatus resultStatus){
		Result result=new Result();
		result
		return
	}*/

	public static <T> Result<T> success(R r) {
		return new Result<>(r, null);
	}

	public static <T> Result<T> success(Enum codeEnum) {
		return new Result<>(ResultCache.get(codeEnum), null);
	}

	/** 业务异常返回业务代码和描述信息 */
	public static <T> Result<T> failure() {
		return new Result<>(ResultStatus.INTERNAL_SERVER_ERROR, null);
	}

	/** 业务异常返回业务代码,描述和返回的参数 */
	public static <T> Result<T> failure(ResultStatus resultStatus) {
		return failure(resultStatus, null);
	}

	public static <T> Result<T> failure(Enum codeEnum) {
		return new Result<>(ResultCache.get(codeEnum), null);
	}
	/** 业务异常返回业务代码,描述和返回的参数 */
	public static <T> Result<T> failure(ResultStatus resultStatus, T data) {
		if (resultStatus == null) {
			return new Result<>(ResultStatus.INTERNAL_SERVER_ERROR, null);
		}
		return new Result<>(resultStatus, data);
	}


	/**
	 * 统一错误返回码，返回错误code和错误信息, 和
	 * @return
	 */
	public static <T> Result<T> failure(R r, T data) {
		return new Result<>(r, data);
	}

	/**
	 * 统一错误返回码，返回错误code和错误信息
	 * 统一错误返回码，被failure(Enum)调用
	 * @return
	 */
	public static <T> Result<T> failure(R r) {
		return new Result<>(r, null);
	}

	public Integer getCode() {
		return code;
	}

	public String getMsg() {
		return msg;
	}

	public T getData() {
		return data;
	}

	@Override
	public String toString() {
		return "Result [code=" + code + ", msg=" + msg + ", data=" + data + "]";
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public void setData(T data) {
		this.data = data;
	}
}
